"""
#!!===========================================================
#版权（C）2023, Huawei Technologies Co., HUTAF xDevice
#!!===========================================================
#文 件 名:      SecurityUserIamAuthWidgetHapApi.py
#文件说明:      认证场景选择公共AW
#作   者:
#生成日期:
#!!===========================================================
"""

# -*- coding: utf-8 -*-
from devicetest.core.test_case import keyword
from devicetest.aw.OpenHarmony import CommonOH
from aw.Useriam.AuthWidget import SecurityUserIamAuthWidgetCommon


@keyword
def Hapinstall(oh_device):
    """
        #======================================================================
        #   @Method:           Hapinstall(oh_device)
        #   @Precondition:     测试Hap安装
        #   @Func:             测试Hap安装
        #   @Param:
        #   @Example:
        #======================================================================
        """
    CommonOH.hdc(oh_device, "hdc install resource\\hap\\AuthWidget.hap")
    CommonOH.hdc(oh_device, "hdc install resource\\hap\\test.hap")
    CommonOH.hdc(oh_device, "hdc install resource\\hap\\test_iam.hap")

@keyword
def setAPI(oh_device, type=0):
    """
    #======================================================================
    #   @Method:           setAPI(oh_device,type)
    #   @Precondition:     选择类型
    #   @Func:             使用测试hap拉起控件
    #   @Param:            type：0:API9,1:API8,2:API6,3:API10,默认值为0
    #   @Example:
    #======================================================================
    """
    SecurityUserIamAuthWidgetCommon.enterhapseting(oh_device)
    if type == 0:
        CommonOH.touchByText(oh_device, "API9 接口测试")
    elif type == 1:
        CommonOH.touchByText(oh_device, "API8 接口测试")
    elif type == 2:
        CommonOH.touchByText(oh_device, "API6 接口测试")
    elif type == 3:
        CommonOH.touchByText(oh_device, "API10 接口测试")
    else:
        print("您输入的type值错误，请输入正确的type值")
        return False

@keyword
def setface(oh_device):
    SecurityUserIamAuthWidgetCommon.enterface_fingerprint_hapseting(oh_device)
    """
    #======================================================================
    #   @Method:           setface(oh_device)
    #   @Precondition:     预置条件
    #   @Func:             预置人脸
    #   @Example:
    #======================================================================
    """

    CommonOH.touchByText(oh_device, "人脸功能测试")
    CommonOH.touchByText(oh_device, "enrollFace")

@keyword
def setVerifyResults(oh_device, type=0):
    CommonOH.hdc(oh_device, "hdc shell mount -o rw,remount /")
    CommonOH.hdc(oh_device, "hdc shell mount -o rw,remount /vendor")
    if type == 0:
        # setfaceauthsuccess
        CommonOH.hdc(oh_device,
                     "hdc file send resource\\faceauth\\face_auth_success\libface_auth_interface_service_1.1.z.so /vendor/lib/")
        CommonOH.hdc(oh_device,
                     "hdc file send resource\\faceauth\\face_auth_success\libface_auth_driver.z.so /vendor/lib/")
        CommonOH.hdc(oh_device, "hdc shell \"sed -i 's/enforcing/permissive/g' /system/etc/selinux/config\"")
        CommonOH.hdc(oh_device, "hdc shell \"cat /system/etc/selinux/config | grep SELINUX=\"")
        CommonOH.safeReboot(oh_device)
    elif type == 1:
        # setfaceauthfail
        CommonOH.hdc(oh_device,
                     "hdc file send resource\\faceauth\\face_auth_lock\libface_auth_interface_service_1.1.z.so /vendor/lib/")
        CommonOH.hdc(oh_device,
                     "hdc file send resource\\faceauth\\face_auth_lock\libface_auth_driver.z.so /vendor/lib/")
        CommonOH.hdc(oh_device, "hdc shell \"sed -i 's/enforcing/permissive/g' /system/etc/selinux/config\"")
        CommonOH.hdc(oh_device, "hdc shell \"cat /system/etc/selinux/config | grep SELINUX=\"")
        CommonOH.safeReboot(oh_device)
    elif type == 2:
        # setfingerprintauthsuccess
        CommonOH.hdc(oh_device,
                     "hdc file send resource\\fingerprint_auth\\fingerprint_auth_success\libfingerprint_auth_interface_service_1.1.z.so /vendor/lib/")
        CommonOH.hdc(oh_device,
                     "hdc file send resource\\fingerprint_auth\\fingerprint_auth_success\libfingerprint_auth_driver.z.so /vendor/lib/")
        CommonOH.safeReboot(oh_device)
    elif type == 3:
        # setfingerprintauthfail
        CommonOH.hdc(oh_device,
                     "hdc file send resource\\fingerprint_auth\\fingerprint_auth_lock\libfingerprint_auth_interface_service_1.1.z.so /vendor/lib/")
        CommonOH.hdc(oh_device,
                     "hdc file send resource\\fingerprint_auth\\fingerprint_auth_lock\libfingerprint_auth_driver.z.so /vendor/lib/")
        CommonOH.safeReboot(oh_device)
    else:
        print("您输入的type值错误，请输入正确的type值")

@keyword
def setfingerprint(oh_device):
    SecurityUserIamAuthWidgetCommon.enterface_fingerprint_hapseting(oh_device)
    """
    #======================================================================
    #   @Method:           setfingerprint(oh_device)
    #   @Precondition:     预置条件
    #   @Func:             预置指纹          
    #   @Example:
    #======================================================================
    """
    CommonOH.touchByText(oh_device, "指纹功能测试")
    CommonOH.touchByText(oh_device, "enroll")

@keyword
def closeselinux(oh_device):
    """
    #======================================================================
    #   @Method:           setface(oh_device)
    #   @Precondition:     重启后设置
    #   @Func:             关闭selinux
    #   @Example:
    #======================================================================
    """
    CommonOH.hdc(oh_device, "hdc shell setenforce 0")

@keyword
def deletface(oh_device):
    SecurityUserIamAuthWidgetCommon.enterface_fingerprint_hapseting(oh_device)
    """
    #======================================================================
    #   @Method:           deletface(oh_device)
    #   @Precondition:     环境恢复
    #   @Func:             删除人脸          
    #   @Example:
    #======================================================================
    """
    CommonOH.touchByText(oh_device, "人脸功能测试")
    CommonOH.touchByText(oh_device, "faceDelCred")

@keyword
def deletefingerprint(oh_device):
    SecurityUserIamAuthWidgetCommon.enterface_fingerprint_hapseting(oh_device)
    """
    #======================================================================
    #   @Method:           deletefingerprint(oh_device)
    #   @Precondition:     环境恢复
    #   @Func:             删除指纹
    #   @Example:
    #======================================================================
    """
    CommonOH.touchByText(oh_device, "指纹功能测试")
    CommonOH.touchByText(oh_device, "delCred")

@keyword
def setAuthType(oh_device, authtype, trustlevel, title, Navigation, WindowModeType):
    """
    #======================================================================
    #   @Method:           setAuthType(oh_device)
    #   @Precondition:     认证类型
    #   @Func:             认证类型选择
    #   @Example:
    #======================================================================
    """
    CommonOH.inputTextByType(oh_device, "TextInput", authtype, 0)
    CommonOH.inputTextByType(oh_device, "TextInput", trustlevel, 1)
    CommonOH.inputTextByType(oh_device, "TextInput", title, 2)
    CommonOH.inputTextByType(oh_device, "TextInput", Navigation, 3)
    CommonOH.inputTextByType(oh_device, "TextInput", WindowModeType, 4)

@keyword
def auth(oh_device, type=0):
    """
    #======================================================================
    #   @Method:           auth(oh_device,type)
    #   @Precondition:     拉起控件
    #   @Func:             拉起控件
    #   @Param:            type：0:默认值,1:调用on函数,2:调用off函数,3:调用start函数,4:调用cancel函数
    #   @Example:
    #======================================================================
    """
    if type == 0:
        CommonOH.touchByText(oh_device, "Auth")
    elif type == 1:
        CommonOH.touchByText(oh_device, "on")
        CommonOH.touchByText(oh_device, "getAuthInstance")
    elif type == 2:
        CommonOH.touchByText(oh_device, "off")
        CommonOH.touchByText(oh_device, "getAuthInstance")
    elif type == 3:
        CommonOH.touchByText(oh_device, "start")
        CommonOH.touchByText(oh_device, "getAuthInstance")
    elif type == 4:
        CommonOH.touchByText(oh_device, "cancel")
        CommonOH.touchByText(oh_device, "getAuthInstance")
    else:
        print("您输入的type值错误，请输入正确的type值")
        return False
